﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace IFTRUE.EIMS.Web
{
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            BLL.UserInfo userInfoBLL = new BLL.UserInfo();
            Model.UserInfo userInfoModel = userInfoBLL.Get(txtAccount.Value.Trim(), txtPwd.Value.Trim());

            if (userInfoModel != null)
            {
                //判断用户已否启用
                if (!userInfoModel.IsUse)
                {
                    lblMsg.Text = "该帐号已被禁用";
                    return;
                }
                //用户信息写入Session
                Model.MySession mySessionModel = new Model.MySession();
                mySessionModel.Id = userInfoModel.Id.ToString();
                mySessionModel.Account = userInfoModel.Account;
                mySessionModel.Name = userInfoModel.Name;
                mySessionModel.LoginTime = DateTime.Now;
                Session["currentuser"] = mySessionModel;

                //修改用户最后登录时间
                userInfoModel.LastDate = DateTime.Now;
                userInfoBLL.Update(userInfoModel, "LastDate", typeof(DateTime));


                //记录登录日志
                Model.Log logModel = new Model.Log();
                logModel.Type = 2;
                logModel.Title="登录";
                logModel.Content = string.Format("用户（{0}），时间（{1}）", userInfoModel.Account, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                logModel.CreateDate = DateTime.Now;
                logModel.IP = Helper.IpHelper.GetClientIP();
                new BLL.Log().Add(logModel);

                MongoDB.Driver.MongoCollection mc = BLL.MongoDbFactory.MongoDatabase.GetCollection("Log");
                MongoDB.Driver.QueryDocument q=new MongoDB.Driver.QueryDocument { { "_id", new MongoDB.Bson.ObjectId("5213318efea05120585c183a") } };
                var v = mc.FindOneAs<Model.Log>(q);

                Response.Redirect("Default.aspx");
            }
            else
            {
                lblMsg.Text = "帐号或密码错误";
            }
        }
    }
}